package com.hooya.fa.eu.biz.dao.manager.special;

import com.alibaba.excel.EasyExcel;
import com.hooya.fa.eu.api.bo.HsCodeBO;
import com.hooya.fa.eu.api.bo.report.StkReportInfoBO;
import com.hooya.fa.eu.api.enums.CompanyEnum;
import com.hooya.fa.eu.api.util.FileUtil;
import com.hooya.fa.eu.biz.dao.entity.MetaProduct;
import com.hooya.fa.eu.biz.dao.mapper.MetaProductMapper;
import com.hooya.fa.eu.biz.dao.mapper.MetaRelationMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.hooya.fa.eu.api.constant.FileNameConstant.IN_OUT_FOLDER_NAME;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/1/3
 * @Version 1.0.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class InoutReportManager {

    private final MetaRelationMapper metaRelationMapper;

    private final MetaProductMapper metaProductMapper;

    public void dealWithDetail(List<StkReportInfoBO> reportInfoList, String belongs,
                               String date, String type) {
        List<HsCodeBO> hsCodeList = metaRelationMapper.getSkuHsCodeList();
        List<MetaProduct> productList = metaProductMapper.selectList(null);
        Map<String, String> belongsResMap = hsCodeList.stream()
                .collect(Collectors.toMap(hc -> hc.getSku() + hc.getBelongs(), HsCodeBO::getHsCode));
        Map<String, HsCodeBO> skuResMap = hsCodeList.stream()
                .collect(Collectors.toMap(HsCodeBO::getSku, i -> i, (k1, k2) -> k1));
        Map<String, MetaProduct> productMap = productList.stream()
                .collect(Collectors.toMap(MetaProduct::getSku, i -> i));
        reportInfoList.forEach(l -> {
            if (l.getSku().equals("AC10004BL")) {
                System.out.println(l);
            }
            l.setDate(date);
            if ("from".equals(type)) {
                l.setAddress(l.getToCountry());
            } else {
                l.setAddress(l.getFromCountry());
            }
            //净重与描述
            MetaProduct product = productMap.get(l.getSku());
            if (Objects.nonNull(product)) {
                l.setNetwt(product.getNetWt());
            }
            //------------------------hsCode赋值------------------------
            //特殊判断
            if (l.getSku().startsWith("GM21901009") || l.getSku().startsWith("GM21901008")) {
                l.setHsCode("6202401090");
                return;
            }
            if (l.getSku().startsWith("GM11901003") || l.getSku().startsWith("GM11902001")) {
                l.setHsCode("6203339000");
                return;
            }
            if (l.getSku().startsWith("GM11903006")) {
                l.setHsCode("6207991000");
                return;
            }
            if (l.getSku().startsWith("HW68675CDK")) {
                l.setHsCode("9403202000");
                return;
            }
            if (l.getSku().equals("ES10260DE-WH")) {
                l.setHsCode("8179899710");
                return;
            }
            if (l.getSku().equals("CB10497BN")) {
                l.setHsCode("9403105100");
                return;
            }
            //正常情况：sku+belongs
            String finalHsCode = "";
            if (belongs.equals(CompanyEnum.COSTWAY_PL.getCompany())
                    || belongs.equals(CompanyEnum.COSTWAY_FR.getCompany())) {
                String hsCode = belongsResMap.get(l.getSku() + belongs);
                if (StringUtils.isNotBlank(hsCode)) {
                    finalHsCode = hsCode;
                } else if (belongs.equals(CompanyEnum.COSTWAY_PL.getCompany())) {
                    finalHsCode = belongsResMap.get(l.getSku() + "globalway");
                }
            }
            if (belongs.startsWith("fds")) {
                finalHsCode = belongsResMap.get(l.getSku() + "fds");
            }
            if (StringUtils.isNotBlank(finalHsCode)) {
                l.setHsCode(finalHsCode);
            } else {
                //正常情况：sku
                HsCodeBO bo = skuResMap.get(l.getSku());
                if (Objects.nonNull(bo)) {
                    l.setHsCode(bo.getHsCode());
                } else {
                    throw new RuntimeException("sku:" + l.getSku() + ",belongs:" + belongs + " 无hsCode");
                }
            }
        });
    }

    /**
     * 生成excel
     *
     * @param reportInfoList 数据
     * @param date           excel名称（日期）
     * @param belongs        货权
     * @param country        国家
     * @param type           类型（from/to）
     */
    public void generateExport(List<StkReportInfoBO> reportInfoList, String date,
                               String belongs, String country, String type) {
        String path = IN_OUT_FOLDER_NAME + belongs + "/" + type + "/";
        File newFile = FileUtil.createNewFile(path, date + ".xlsx");
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(newFile.getAbsolutePath(), StkReportInfoBO.class).sheet(country).doWrite(reportInfoList);
    }

}
